package com.yztc.youbadbad.presenter;

import com.yztc.youbadbad.bean.homepage.DeserveBean;
import com.yztc.youbadbad.model.DeserveModelImpl;
import com.yztc.youbadbad.model.IDeserveModel;
import com.yztc.youbadbad.ui.homepage.interfaces.IDeserveView;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Tiffany.V.Chou on 2016/11/15.
 */

public class DeservePresenterImpl implements IDeservePresenter {
    private IDeserveModel iTuijianModel= null;
    private IDeserveView iTuijianView= null;

    public DeservePresenterImpl(IDeserveView iTuijianView) {
        this.iTuijianView = iTuijianView;
        iTuijianModel = new DeserveModelImpl();
    }

    @Override
    public void getDeserveData(int page) {
        iTuijianModel.getDeserveObservable(page)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<DeserveBean>() {
                    @Override
                    public void onCompleted() {
                        iTuijianView.hideProgressBar();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iTuijianView.showErrorMessage();
                        iTuijianView.hideProgressBar();
                    }

                    @Override
                    public void onNext(DeserveBean deserveBean) {
                        iTuijianView.getDeserveData(deserveBean);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        iTuijianView.showProgressBar();

                    }
                });
    }

}
